#!/usr/bin/env python

def _(s): return s

import os, wx, sys, thread, mutex
import wx.lib.mixins.listctrl  as  listmix
import ViewSite

class SiteList(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
    def __init__(self, parent, files, names, OnOpenPage):
        wx.ListCtrl.__init__(self, parent, style=wx.LC_REPORT |
                             wx.LC_SORT_DESCENDING)
        listmix.ListCtrlAutoWidthMixin.__init__(self)
        self.OnOpenPage = OnOpenPage
        self.InsertColumn(0, "Name")
        self.InsertColumn(1, "Owner")
        self.InsertColumn(2, "Permissions")
        
        self.dct = {}
        
        for i in range(len(files)):
            item = wx.ListItem()
            self.dct[i] = files[i]
            item.SetData(i)
            index = self.InsertItem(item)
            self.SetStringItem(index, 0, names[i])
            self.SetStringItem(index, 1, str(i))
            self.SetStringItem(index, 2, files[i])
        self.SortItems(self.doSort)
        
#        self.SetColumnWidth(0, wx.LIST_AUTOSIZE)
#        self.SetColumnWidth(1, wx.LIST_AUTOSIZE)
        self.SetColumnWidth(2, 238)
        self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated)
        
    
    def doSort(self, one, two):
        one = self.dct[one]
        two = self.dct[two]
        if one == two:
            return 0
        elif one < two:
            return -1
        else:
            return 1
    
    def OnItemActivated(self, evt):
        currentItem = evt.m_itemIndex
        item = self.GetItem(currentItem)
        self.OnOpenPage(self.dct[item.GetData()])
    
    def OnSize(self, evt):
        w, h = self.GetParent().GetSize()
        self.SetSize((w, h))
    
class Sites(wx.Notebook):
    def __init__(self, parent, files, names):
        wx.Notebook.__init__(self, parent, style=wx.NB_TOP | wx.CLIP_CHILDREN)
        self.AddPage(SiteList(self, files, names, self.OnOpenPage),
                     _("Site Management"))
        
        isz = (16, 16)
        il = wx.ImageList(isz[0], isz[1])
        self.idx1 = il.Add(wx.ArtProvider.GetBitmap(wx.ART_ADD_BOOKMARK,
                                               wx.ART_FRAME_ICON, isz))
        self.idx2 = il.Add(wx.ArtProvider.GetBitmap(wx.ART_GO_DIR_UP,
                                               wx.ART_FRAME_ICON, isz))
        self.AssignImageList(il)
        self.SetPageImage(0, self.idx1)
        
        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Bind(wx.EVT_CHAR, self.OnChar)
        self.selection = 0
    
    def OnSize(self, evt):
        self.SetSize(self.GetParent().GetClientSize())
    
    def OnOpenPage(self, dir):
        exists = False
        for pos in range(self.GetPageCount()):
            if dir == self.GetPageText(pos):
                exists = True
                break
        if exists:
            self.SetSelection(pos)
        else:
            self.AddPage(ViewSite.ViewSite(self, dir), dir, True)
#            self.SetPageImage(self.GetPageCount() - 1, self.idx2)
    
    def OnChar(self, evt):
        if evt.GetKeyCode() == 23 and self.GetSelection() != 0:
            if self.GetSelection() >= self.GetPageCount():
                self.DeletePage(self.GetPageCount() - 1)
            else:
                self.DeletePage(self.GetSelection())
        evt.skip()
    
if __name__ == "__main__":
    app = wx.PySimpleApp()
    f = wx.Frame(None)
    dir = "/var/www"
    names = os.listdir(dir)
    files = [os.path.join(dir, file) for file in names]
    Sites(f, files, names)
    f.Center()
    f.Show()
    app.MainLoop()
    